<template>
  <div class="search">
    <div class="title-nav">
      <span>搜索</span>
    </div>
    <!-- 搜索框 -->
    <div>
      <van-search
        v-model="value"
        show-action
        placeholder="请输入商家或商品名字"
        @search="onSearch"
      >
        <template #action>
          <div @click="onClickButton">搜索</div>
        </template>
      </van-search>
    </div>
    <!-- 商家列表 -->
    <div class="business-list" v-if="isHas">
      <div class="business">
        <h4>商家</h4>
      </div>
      <ul class="searchlist">
        <li
          v-for="(item, index) in list"
          :key="index"
          @click="goDetail(item.id)"
        >
          <div class="item-img">
            <img
              class="auto-img"
              :src="'//elm.cangdu.org/img/' + item.image_path"
              alt=""
            />
          </div>
          <div>
            <div>
              <span class="business-name">{{ item.name }}</span>
              <span class="label">支付</span>
            </div>
            <div>
              <p>
                月售 <span>{{ item.rating_count }}</span> 单
              </p>
            </div>
            <div>
              <p>
                {{ item.float_minimum_order_amount }}元起送/距离{{
                  item.distance
                }}
              </p>
            </div>
          </div>
        </li>
      </ul>
    </div>
    <div class="txt" v-if="isShow">
      <h3>没有商铺</h3>
    </div>
    <!-- 历史 -->
    <div class="history" v-if="isNull">
      <div class="business">
        <h4>历史列表</h4>
      </div>
      <ul class="historylist">
        <li v-for="(item, index) in listHis" :key="index">
          <span ref="spanDom" @click="getText">{{ item }}</span>
        </li>
        <li class="btn" @click="getnull">
          <div class="buttom">清空所有历史</div>
        </li>
      </ul>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, onMounted, nextTick } from "vue";

import { useRouter } from "vue-router";

import axios from "axios";

export default defineComponent({
  name: "Search",
  setup() {
    let router: any = useRouter();

    const value = ref("");
    // 获取本地存储数据
    let historyList: any = localStorage.getItem("historyList");
    if (historyList) {
      historyList = JSON.parse(historyList);
    } else {
      historyList = [];
    }

    let listHis = ref<Array<any>>(historyList);

    let isHas = ref<boolean>(false);

    let isShow = ref<boolean>(false);

    let isNull = ref<boolean>(false);

    let list: any = ref<Array<any>>([]);

    //获取文本
    let spanDom: any = ref<any>(null);

    // 搜索商家
    let getSearch: (val: any) => void = (val: any): void => {
      axios({
        method: "get",
        url: "https://elm.cangdu.org/v4/restaurants?geohash=23.12908,113.264359",
        params: {
          keyword: val,
        },
      })
        .then((res: any): void => {
          list.value = res.data;
          // console.log('res ==> ',res);
          console.log(list);

          if (
            list.value.type == "ERROR_DATA" ||
            list.value.type == "ERROR_PARAMS"
          ) {
            isShow.value = true;
            isHas.value = false;
            isNull.value = true;
          } else {
            isHas.value = true;
            isNull.value = false;
            isShow.value = false;
          }
          if (value.value === "") {
            isShow.value = false;
            isNull.value = true;
          }
        })
        .catch((err: any): void => {
          console.log("err ==> ", err);
        });
    };
    // 收藏历史
    let getHistory: (val: any) => void = (val: any): void => {
      if (val === "") {
        return;
      }
      if (historyList.length === 0) {
        historyList.push(val);

        listHis.value = historyList;

        localStorage.setItem("historyList", JSON.stringify(historyList));
      }
      let history: any = localStorage.getItem("historyList");
      history = JSON.parse(history);
      history.push(val);

      let arr: any[] = [];
      for (let i = 0; i < history.length; i++) {
        if (arr.indexOf(history[i]) === -1) {
          arr.push(history[i]);
        }
      }
      historyList = arr;

      listHis.value = historyList;

      localStorage.setItem("historyList", JSON.stringify(historyList));
    };

    let goDetail: (id: number) => void = (id: number): void => {
      router.push({
        name: "Detail",
        query: {
          id: id,
        },
      });
      let shop: any = {
        shopsId: id,
        shops: [],
      };
      let shopData: any = localStorage.getItem("shopCart");
      if (shopData) {
        shopData = JSON.parse(shopData);
      } else {
        shopData = [];
      }
      for (let i: number = 0; i < shopData.length; i++) {
        if (shopData[i].shopsId === shop.shopsId) {
          return;
        }
      }
      shopData.push({ ...shop });
      localStorage.setItem("shopCart", JSON.stringify(shopData));
    };

    // 删除所有数据
    let getnull: () => void = (): void => {
      listHis.value = [];
      localStorage.setItem("historyList", JSON.stringify(listHis.value));
      isNull.value = false;
    };

    // 获取节点
    let getText: () => void = (): void => {
      // console.log('spanDom.value==>',spanDom.value);
    };

    // 回车搜索
    const onSearch = (val: any): void => {
      getSearch(val);
      getHistory(val);
    };
    // 点击搜索
    const onClickButton = (): void => {
      getSearch(value.value);
      getHistory(value.value);
    };

    onMounted(() => {
      nextTick(() => {
        // console.log('spanDom.value ==> ', spanDom.value);
      });
    });

    return {
      isShow,
      isHas,
      // ref数据
      value,
      list,
      listHis,
      isNull,

      // 方法
      onSearch,
      onClickButton,
      getnull,
      getText,
      goDetail,
    };
  },
});
</script>

<style lang="scss" scoped>
.auto-img {
  width: 100%;
  display: block;
}
.title-nav {
  width: 100%;
  height: 40px;
  position: sticky;
  top: 0;
  left: 0;
  line-height: 40px;
  color: #fff;
  text-align: center;
  font-size: 18px;
  background-color: #3190e8;
}
.historylist {
  padding: 10px 20px;
  li {
    display: flex;
    justify-content: space-between;
    padding: 10px 5px;
    background-color: #eee;
    margin-bottom: 4px;
    .del {
      display: inline-block;
      width: 40px;
      text-align: center;
      background-color: #f00;
      color: #fff;
    }
  }
  .btn {
    color: #fff;
    font-size: 16px;
    background-color: #fff;
    .buttom {
      width: 200px;
      height: 40px;
      line-height: 40px;
      text-align: center;
      border-radius: 10px;
      margin: 0 auto;
      background-color: #3190e8;
    }
  }
}
.txt {
  text-align: center;
  font-size: 16px;
  background-color: #eee;
  padding: 10px 10px;
  h3 {
    margin: 0;
    font-weight: normal;
  }
}
.business {
  width: 100%;
  padding: 5px;
  box-sizing: border-box;
  border-bottom: 2px dashed #ddd;
  h4 {
    margin: 5px 0;
    font-size: 16px;
    color: #555;
    font-weight: bold;
  }
}
.searchlist {
  width: 100%;
  padding: 5px;
  box-sizing: border-box;
  margin-bottom: 50px;
  li {
    width: 100%;
    display: flex;
    align-items: center;
    margin-bottom: 10px;
    .business-name {
      font-size: 14px;
    }
    .label {
      display: inline-block;
      margin-left: 10px;
      font-size: 12px;
      border: 1px solid #ff6000;
      color: #ff6000;
    }
    p {
      margin: 3px 0;
    }
    .item-img {
      width: 50px;
      height: 50px;
      margin: 5px;
      background-color: #ff8181;
    }
  }
}
</style>